/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 29.07.2005
 *
 *************************************************/
package fightplan.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import fightplan.business.AbstractFighter;
import fightplan.business.Category;
import fightplan.business.Fight;
import fightplan.business.FightEntry;
import fightplan.core.Style;
import fightplan.views.tables.ResultCellEditor;

@SuppressWarnings("serial")
@Deprecated
public class FightResultsPanel extends JPanel {
	private Category category;

	public FightResultsPanel(Category category) {
		this.category = category;

		setLayout(new BorderLayout());

		final JTable fightsTable = new JTable(new FightsTableModel());
		TableColumnModel cm = fightsTable.getColumnModel();
		cm.getColumn(0).setPreferredWidth(60);
		cm.getColumn(0).setMinWidth(60);
		cm.getColumn(0).setMaxWidth(60);
		cm.getColumn(1).setPreferredWidth(110);
		cm.getColumn(1).setMinWidth(110);
		cm.getColumn(1).setMaxWidth(110);
		fightsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

		// final FightResultTableModel fightResultTableModel = new
		// FightResultTableModel();

		final BeanListTableModel<FightEntry> fightResultTableModel = new BeanListTableModel<FightEntry>(null,
				new String[] { "fighterProvider.fighter.displayName", "result" }, new String[] { "Teilnehmer",
						"Resultat" });
		fightResultTableModel.setColumnReadOnly(0);

		JTable resultTable = new JTable(fightResultTableModel);
		resultTable.getColumnModel().getColumn(1).setCellEditor(new ResultCellEditor());

		FormLayout layout = new FormLayout("left:max(40dlu;p), 4dlu, pref:grow(1), 7dlu, ",
				"20dlu, 100px:grow:fill, 20dlu, 80px:grow:fill");

		DefaultFormBuilder builder = new DefaultFormBuilder(layout);

		builder.setDefaultDialogBorder();

		builder.appendSeparator("Begegnungen");

		JScrollPane scrollPane = new JScrollPane(fightsTable);
		scrollPane.getViewport().setBackground(Style.UNUSED_TABLE_SPACE);
		scrollPane.setPreferredSize(new Dimension(5000, 5000));
		builder.append(scrollPane, 3);

		builder.appendSeparator("Resultat");

		scrollPane = new JScrollPane(resultTable);
		scrollPane.getViewport().setBackground(Style.UNUSED_TABLE_SPACE);
		scrollPane.setPreferredSize(new Dimension(5000, 5000));
		builder.append(new JScrollPane(resultTable), 3);

		add(builder.getPanel(), BorderLayout.CENTER);

		fightsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

			public void valueChanged(ListSelectionEvent e) {
				if (fightsTable.getSelectionModel().isSelectionEmpty()) {
					fightResultTableModel.setList(null);
				} else {
					List<Fight> fights = FightResultsPanel.this.category.getPlan().getFights();
					fightResultTableModel.setList(fights.get(fightsTable.getSelectionModel().getLeadSelectionIndex())
							.getFightEntries());
				}
			}

		});
	}

	private class FightsTableModel extends AbstractTableModel {

		public int getColumnCount() {
			return 4;
		}

		@Override
		public String getColumnName(int column) {
			if (column == 0)
				return "Blatt";
			if (column == 1)
				return "Name";
			if (column == 2)
				return "Teilnehmer";
			if (column == 3)
				return "Ergebnis der Begegnung";
			return "";
		}

		public int getRowCount() {
			return category.getPlan().getFights().size();
		}

		public Object getValueAt(int rowIndex, int columnIndex) {

			if (columnIndex == 0) {
				Fight fight = category.getPlan().getFights().get(rowIndex);
				int sheet = category.getLayout().getSheetIndexForFight(fight);
				return sheet >= 0 ? "Blatt " + (sheet + 1) : "-";
			}
			if (columnIndex == 1) {
				Fight fight = category.getPlan().getFights().get(rowIndex);
				if (fight.getName() != null) {
					return fight.getName();
				}
			} else if (columnIndex == 2) {
				Fight fight = category.getPlan().getFights().get(rowIndex);
				if (fight.getFighterCount() == 2) {
					return format(fight, 0) + " - " + format(fight, 1);
				} else {
					String s = "";
					for (int i = 0; i < fight.getFighterCount(); i++) {
						s += format(fight, i);
						if (i < fight.getFighterCount() - 1) {
							s += ", ";
						}
					}
					return s;
				}
			} else if (columnIndex == 3) {
				Fight fight = category.getPlan().getFights().get(rowIndex);
				return fight.getDisplayResult();
			}

			return "";
		}

		private String format(Fight fight, int index) {
			AbstractFighter fighter = fight.getFightEntries().get(index).getFighterProvider().getFighter();
			if (fighter == null) {
				return "?";
			} else {
				return fighter.getDisplayName();
			}
		}

	}

}